//
//  TRCommentTableViewCell.m
//  WeiMiLan
//
//  Created by anan on 14/11/20.
//  Copyright (c) 2014年 Mac. All rights reserved.
//
#import "DLAVAlertView.h"
#import "TRCommentTableViewCell.h"

@implementation TRCommentTableViewCell

- (void)awakeFromNib {
    // Initialization code
}
- (IBAction)praiseAction:(UIButton *)sender {
    

    NSString *tuhmbs_up_flag = [NSString stringWithFormat:@"%@",self.info[@"THUMBS_UP_FLAG"]];
    NSString *thumbs_up_type = nil;
    
    if ([tuhmbs_up_flag isEqualToString:@"yes"]) {
        
        thumbs_up_type = @"10009";
        
    }else {
        thumbs_up_type = @"10008";
    }
    
    
    NSString *userId = [NSStandardUserDefaults objectForKey:@"USER_ID"];
    NSDictionary *parameters = @{@"userId" : userId,
                                 @"type" : thumbs_up_type,
                                 @"commentId" : [NSString stringWithFormat:@"%zd",self.replyBtn.tag]
                                 };
    
    AFHTTPRequestOperationManager *requestManager = [AFHTTPRequestOperationManager manager];
    requestManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [SVProgressHUD show];
    //int productId = [self.info ql_intForKey:@"ID"];
    [requestManager POST:@"http://ping.vsmu.cn/app/Interface/Comment" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        if ([dic ql_intForKey:@"code"]==0000) {
            if ([thumbs_up_type isEqualToString:@"10008"]) {
                [self.praiseImageButton setBackgroundImage:[UIImage imageNamed:@"赞"] forState:UIControlStateNormal];
                [SVProgressHUD showSuccessWithStatus:@"点赞成功"];
            } else if ([thumbs_up_type isEqualToString:@"10009"]) {
                [self.praiseImageButton setBackgroundImage:[UIImage imageNamed:@"iconfont-zan-(5)"] forState:UIControlStateNormal];
                [SVProgressHUD showSuccessWithStatus:@"取消点赞成功"];
            }
            
        }
        [[NSNotificationCenter defaultCenter]postNotificationName:@"kPrasieAction" object:nil userInfo:nil];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if ([thumbs_up_type isEqualToString:@"10008"]) {
            [self.praiseImageButton setBackgroundImage:[UIImage imageNamed:@"iconfont-zan-(5)"] forState:UIControlStateNormal];
            [SVProgressHUD showErrorWithStatus:@"点赞失败"];
        } else if([thumbs_up_type isEqualToString:@"10009"]){
            [self.praiseImageButton setBackgroundImage:[UIImage imageNamed:@"赞"] forState:UIControlStateNormal];
            [SVProgressHUD showErrorWithStatus:@"取消点赞失败"];
        }
        
         [[NSNotificationCenter defaultCenter]postNotificationName:@"kPrasieAction" object:nil userInfo:nil];
    }];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (void)setReplyStr:(NSString *)replyStr {
    _replyStr = replyStr;
}

- (void)setInfo:(NSDictionary *)info
{
    _info = info;
  //  NSLog(@"%@",info);
    if (info == nil) {
        return;
    }
    self.imageViewUserHeader.layer.masksToBounds = YES;
    self.imageViewUserHeader.layer.cornerRadius = self.imageViewUserHeader.width * 0.5; //设置图片圆角的尺度
    [self.imageViewUserHeader sd_setImageWithURL:[NSURL URLWithString:info[@"AUTHOR_HEAD_PIC"]] placeholderImage:[UIImage imageNamed:@"nodata_userheader"]];

    self.labelUserName.text = info[@"AUTHOR_NAME"];
    self.labelContent.text = info[@"CONTENT"];
    self.replyBtn.tag = [info[@"ID"] integerValue];
    
    
    if ([[info allKeys] containsObject:@"THUMBS_UP"]) {
        
        NSInteger praiseCount = [info[@"THUMBS_UP"] integerValue];
        if (praiseCount == 0) {
            
            [self.praiseImageButton setBackgroundImage:[UIImage imageNamed:@"iconfont-zan-(5)"] forState:UIControlStateNormal];
        } else if (praiseCount > 0) {
            self.priseLabel.text = [NSString stringWithFormat:@"%zd",praiseCount];
            [self.praiseImageButton setBackgroundImage:[UIImage imageNamed:@"赞"] forState:UIControlStateNormal];
        }
    }
    

   // NSLog(@"%zd",self.replyBtn.tag);
    //回复
    if ([info ql_hasObjectForKey:@"ANSWER_ID"]) {
        NSString *header = [NSString stringWithFormat:@"回复%@: ",info[@"ANSWER_USERNAME"]];
       
        self.replyCount.text = [NSString stringWithFormat:@"%zd",[info ql_hasObjectForKey:@"REPLIES"]];
        
        NSString *content = [NSString stringWithFormat:@"%@%@",header,info[@"CONTENT"]];
        self.labelContent.text = content;
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:content];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithWhite:0.600 alpha:1.000] range:NSMakeRange(0,header.length)];
        self.labelContent.attributedText = str;
    }
    self.labelTime.text = self.info[@"CREATE_DATE"];

}

- (float)getHeight
{
    return self.frame.size.height + 6;
}

- (IBAction)buttonUserDetailAction:(id)sender
{
    [QLPlayerManager openUserHomeWithId:[self.info ql_intForKey:@"AUTHOR_ID"] viewController:self.viewController];
}

- (IBAction)replyAction:(UIButton *)sender {
    
  //  NSLog(@"%@++++++++",sender);
    
}
@end
